package com.cos.controller;

import com.cos.constant.LoginSuccess;

import com.cos.constant.RedissonLockConstant;
import com.cos.service.api.HomeService;
import com.cos.utils.JsonUtils;
import com.cos.entity.GoodsTypes;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    @Autowired
    private HomeService homeService;
    @Autowired
    private RedissonClient client;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping ({"/index.html", "/"})
    //这里HttpSession 是一样的
    public String index(ModelMap modelMap,HttpServletRequest request){

        //去redis中查询是否存在数据
        String list1 = redisTemplate.opsForValue().get(RedissonLockConstant.HOME_GOODS_KEY);
        if (StringUtils.isEmpty(list1)) {
            list1=goodsTypeList();
        }
        //把json字符串转成list对象集合
        List<GoodsTypes> list= JsonUtils.toList(list1,List.class);
        modelMap.put("goodsTypeList",list);
        Object attribute = request.getSession().getAttribute(LoginSuccess.LOGIN_SUCCESS);
        System.out.println(attribute);
        return "index";

    }



    public String goodsTypeList() {
        //创建一把锁
        RLock lock = client.getLock(RedissonLockConstant.HOME_REDISSON_LOCK_KEY);
        //加锁
        lock.lock();
        try {
            String s = redisTemplate.opsForValue().get(RedissonLockConstant.HOME_GOODS_KEY);
            if (!StringUtils.isEmpty(s)) {
                return s;
            }
            List<GoodsTypes> list = homeService.getList();
            //创建一个集合来装父节点
            List<GoodsTypes> list1 = new ArrayList<>();
            for (GoodsTypes goodsTypes : list) {
                if (goodsTypes.getGoodsTypePid() == -1) {
                    list1.add(goodsTypes);
                }
            }
            for (GoodsTypes goodsTypes : list1) {
                for (GoodsTypes types : list) {
                    if (goodsTypes.getId().equals(types.getGoodsTypePid())) {
                        goodsTypes.getChildren().add(types);
                    }
                }
            }
            //转成json能够跨平台使用
            String s1 = JsonUtils.toJson(list1);
            if (StringUtils.isEmpty(s1)) {
                s1="null";
                redisTemplate.opsForValue().set(RedissonLockConstant.HOME_GOODS_KEY,s1);
            }
            //将数据存入Redis中
            redisTemplate.opsForValue().set(RedissonLockConstant.HOME_GOODS_KEY, s1, 10, TimeUnit.DAYS);
            return s1;
        } finally {
            lock.unlock();
        }

    }
}
